<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column align="center" label="索引" width="150">
          <template slot-scope="scope">
            <span>
              {{ scope.$index + 1 + (obj.current - 1) * obj.pageSize }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="stem" label="标题" width="400">
        </el-table-column>
        <el-table-column align="center" prop="creator" label="作者">
        </el-table-column>
        <el-table-column align="center" prop="likeCount" label="点赞">
        </el-table-column>
        <el-table-column align="center" prop="views" label="浏览数" width="120">
        </el-table-column>
        <el-table-column
          align="center"
          prop="createdAt"
          label="更新时间"
          width="200"
        >
        </el-table-column>
        <el-table-column label="操作" width="120  " align="center">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              icon="el-icon-view"
              @click="openDrawer('preview', scope.row.id)"
            ></el-button>
            <el-button
              @click="openDrawer('edit', scope.row.id)"
              icon="el-icon-edit-outline"
              type="text"
              size="small"
            ></el-button>
            <el-button
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="obj.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="sizes, prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </div>
      <el-drawer
        :visible.sync="isShowDrawer"
        :before-close="handleClose"
        :title="drawerTitle"
        direction="rtl"
      >
        <el-form label-width="80px">
          <el-form-item label="标题">
            <el-input placeholder="输入面经标题"></el-input
          ></el-form-item>
          <el-form-item label="内容"> 富文本编辑器</el-form-item>
          <el-form-item>
            <el-button type="primary">确认</el-button
            ><el-button @click="isShowDrawer = false">取消</el-button>
          </el-form-item></el-form
        >
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
import { articleApi, deleteApi } from "@/api/user.js";
export default {
  name: "article-page",
  created() {
    this.getArticleData();
  },
  computed: {
    drawerTitle() {
      let title = "大标题";
      if (this.drawerType === "add") title = "添加面经";
      if (this.drawerType === "preview") title = "面经预览";
      if (this.drawerType === "edit") title = "修改面经";
      return title;
    },
  },
  methods: {
    async getArticleData() {
      const res = await articleApi(this.obj);
      // console.log(res);
      this.tableData = res.data.data.rows;
      this.total = res.data.data.total;
    },
    async handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteApi(id);
          // console.log(res);
          this.getArticleData(this.obj);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {});
    },

    handleSizeChange(val) {
      this.obj.pageSize = val;
      this.getArticleData(this.obj);
    },
    handleCurrentChange(val) {
      this.obj.current = val;
      this.getArticleData(this.obj);
    },
    openDrawer(type, id) {
      // console.log(type, id)
      this.drawerType = type;
      this.isShowDrawer = true;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.drawer = false;
          done();
        })
        .catch((_) => {});
    },
  },

  data() {
    return {
      tableData: [],
      total: 0,
      obj: {
        current: 1,
        pageSize: 10,
      },
      isShowDrawer: false,
      direction: "rtl",
      drawerType: "",
      // tableData: [
      //   {
      //     date: "2016-05-02",
      //     name: "王小虎",
      //     province: "上海",
      //     city: "普陀区",
      //     address: "上海市普陀区金沙江路 1518 弄",
      //     zip: 200333,
      //   },
      //   {
      //     date: "2016-05-04",
      //     name: "王小虎",
      //     province: "上海",
      //     city: "普陀区",
      //     address: "上海市普陀区金沙江路 1517 弄",
      //     zip: 200333,
      //   },
      //   {
      //     date: "2016-05-01",
      //     name: "王小虎",
      //     province: "上海",
      //     city: "普陀区",
      //     address: "上海市普陀区金沙江路 1519 弄",
      //     zip: 200333,
      //   },
      //   {
      //     date: "2016-05-03",
      //     name: "王小虎",
      //     province: "上海",
      //     city: "普陀区",
      //     address: "上海市普陀区金沙江路 1516 弄",
      //     zip: 200333,
      //   },
      // ],
    };
  },
};
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
